package com.online.shopping.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.online.shopping.mapper.TbSpecificationOptionMapper;
import com.online.shopping.pojo.*;
import com.online.shopping.service.SpecificationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Rui
 * @version 1.0
 * @date 2020/11/12 15:35
 */
@Api(tags = "规格管理", description = "规格增删改查")
@RestController
@Slf4j
@RequestMapping("/manager/specification")
@CrossOrigin
public class SpecificationController {

    @Autowired
    private SpecificationService specService;

    @Autowired
    private TbSpecificationOptionMapper specificationOptionMapper;

    @ApiOperation("添加规格")
    @PostMapping("/addSpecComb")
    public CommonResult addSpecComb(@RequestBody SpecComb specComb){
        try {
            specService.addSpecification(specComb.getSpecification());
            for (TbSpecificationOption tbSpecificationOption : specComb.getSpecificationOptionList()) {
                tbSpecificationOption.setSpecId(specComb.getSpecification().getId());
                specificationOptionMapper.insert(tbSpecificationOption);
            }
            return new CommonResult(200, "添加成功！", specComb);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult(400, "添加失败！");
        }
    }

    @ApiOperation("更新规格")
    @PostMapping("/updateSpecComb")
    public CommonResult updateSpecComb(@RequestBody SpecComb specComb){
        try {
            specService.updateSpecification(specComb.getSpecification());

            TbSpecificationOptionExample example = new TbSpecificationOptionExample();
            TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
            criteria.andSpecIdEqualTo(specComb.getSpecification().getId());

            specificationOptionMapper.deleteByExample(example);

            for (TbSpecificationOption specificationOption : specComb.getSpecificationOptionList()) {
                specificationOption.setSpecId(specComb.getSpecification().getId());
                specificationOptionMapper.insert(specificationOption);
            }
            return new CommonResult(200, "修改成功！", specComb);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult(400, "修改失败！");
        }
    }

//    @GetMapping("/delSpecComb")
    public CommonResult delSpecComb(Long id){
        try {
            specService.deleteSpecification(id);

            TbSpecificationOptionExample example = new TbSpecificationOptionExample();
            TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
            criteria.andSpecIdEqualTo(id);
            specificationOptionMapper.deleteByExample(example);
            return new CommonResult(200, "删除成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult(400, "删除失败!");
        }
    }

    @ApiOperation("删除多个规格")
    @GetMapping("/delManySpecComb")
    public CommonResult delManySpecComb(Long[] ids){
        try {
            for (Long id : ids) {
                delSpecComb(id);
            }
            return new CommonResult(200, "删除成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult(400, "删除失败！");
        }
    }

    @ApiOperation("根据id查找规格")
    @GetMapping("/findSpecCombById/{id}")
    public SpecComb findOne(@PathVariable("id") Long id){
        SpecComb specification = new SpecComb();
        // 根据规格ID查询规格对象
        TbSpecification tbSpecification = specService.findSpecificationById(id);
        specification.setSpecification(tbSpecification);

        // 根据规格的ID查询规格选项
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
        criteria.andSpecIdEqualTo(id);

        List<TbSpecificationOption> list = specificationOptionMapper.selectByExample(example);
        specification.setSpecificationOptionList(list);

        log.info(String.valueOf(specification));

        return specification;
    }

    @ApiOperation("查询所有规格")
    @GetMapping("/findAllSpecs")
    public List<TbSpecification> findALlSpecs(){
        List<TbSpecification> specifications = specService.findAllSpecifications();
        return specifications;
    }

    @ApiOperation("分页")
    @GetMapping("/findByPage")
    public ResultPage findByPage(int pageNum, int pageSize){
        PageHelper.startPage(pageNum, pageSize);
        Page<TbSpecification> page = (Page<TbSpecification>) specService.findAllSpecifications();

        ResultPage resultPage = new ResultPage();
        resultPage.setRows(page.getResult());
        resultPage.setTotal(page.getTotal());

        return resultPage;
    }
}
